<template>
	<view>
		<u-form-item label="机身码" prop="code">
			<view class="flex">
				<view><u-input v-model="form.code" placeholder="请输入机身码" @blur="codeInput"></u-input></view>
				<view><text @click="scanClick" class="cuIcon-scan padding-left-xs" style="font-size: 38rpx;"></text></view>
			</view>
		</u-form-item>
		<u-form-item label="型号" prop="model"><u-input v-model="form.model" placeholder=" " :disabled="true"></u-input></u-form-item>
		<u-form-item label="颜色" prop="color"><u-input v-model="form.color" placeholder=" " :disabled="true"></u-input></u-form-item>

		<u-form-item v-if="needDetail" label="出厂日期" prop="createTime"><u-input v-model="form.createTime" placeholder=" " :disabled="true"></u-input></u-form-item>
		<u-form-item v-if="needDetail" label="销售日期" prop="saleTime"><u-input v-model="form.saleTime" placeholder=" " :disabled="true"></u-input></u-form-item>
		<u-form-item v-if="needDetail" label="出货经销商" prop="saleDealer">
			<u-input v-model="form.saleDealer" placeholder=" " :disabled="true"></u-input>
		</u-form-item>
		<u-form-item v-if="needDetail" label="是否厂家保修" prop="insurance">
			<!--19190321 v-if="form.insurance==true"-->
			<u-input  v-model="form.insurance" placeholder=" " :disabled="true">
			</u-input>
		</u-form-item>
	</view>
</template>

<script>
import { mapActions, mapMutations } from 'vuex';
import { CompanyId, EntId } from '../../../../config/t100.js';
export default {
	props: {
		needDetail: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			result:'',
			form: {
				code: '',
				type: '',
				color: '',
				createTime: '',
				saleTime: '',
				saleDealer: '',
				insurance:'',
			}
		};
	},
	methods: {
		...mapMutations({
			updateScanDeviceInfo: 'storageOrder/updateScanDeviceInfo',
			
		}),
		...mapActions({
			getDeviceInfoFromT100: 't100/getDeviceInfoFromT100',
			getDeviceByCustomer: 'storageOrder/getDeviceByCustomer',
			getAgentByTeamNo: 'agent/getAgentByTeamNo',
			getOrderReport:'storageOrder/getOrderReport',
		}),
		resetForm() {
			this.form = {
				code: '',
				model: '',
				color: '',
				createTime: '',
				saleTime: '',
				saleDealer: '',
				insurance:'',
			};
		},
		scanClick() {
			let self = this;
			uni.scanCode({
				success(res) {
					if (res && res.result) {
						self.form.code = res.result;
						self.codeInput(res.result);
					}
				},
				fail(err) {
					console.log(err);
					if (err && err.errMsg != 'scanCode:fail cancel') {
						uni.showToast({
							title: `扫码失败，请重新扫码`,
							icon: 'none'
						});
					}
				}
			});
		},
		checkCodeInList(code) {
			let list = this.$store.state.storageOrder.sumList;
			return list.some(e => {
				return e.code == code;
			});
		},
		codeInput(e) {
			console.log(e);
			if (e) {
				if (this.checkCodeInList(e) && !this.needDetail)
					return uni.showToast({
						title: '该机身码已扫',
						icon: 'none'
					});
				uni.showLoading({
					title: '请求中'
				});
				this.getDeviceInfoFromT100(e)
					.then(res => {
						console.log(res);
						uni.hideLoading();
						let isValid = false;
						
						if (res && res.payload && res.payload.std_data && res.payload.std_data.execution && res.payload.std_data.execution.code == '0') {
							if (res && res.payload && res.payload.std_data && res.payload.std_data.parameter && res.payload.std_data.parameter.delivery_ordem_info) {
								let arr = res.payload.std_data.parameter.delivery_ordem_info;
								if (Array.isArray(arr)) {
									this.form.model = '';
									this.form.color = '';
									for (let i = 0; i < arr.length; i++) {
										if (arr[i].midxucsite == CompanyId && arr[i].midxucent == EntId) {
											if (arr[i].imaal004) {
												let strArr = arr[i].imaal004.split('/');
												this.form.model = strArr[0];
												this.form.color = strArr[1];
												isValid = true;
											}
											this.form.createTime = arr[i].midxuc010;
											// if(arr[i].midxuc012){
											// 	this.form.saleDealer = arr[i].midxuc012
											// }
											break;
										}
									}
								}
								if (isValid && this.needDetail) {
									this.getProductFromServer(e);
								}else{
									this.form.saleTime = '';
									this.form.saleDealer = '';
								}
							} else {
								this.form.model = '';
								this.form.color = '';
								this.form.createTime = '';
								uni.showToast({
									title: '获取机身码信息失败',
									icon: 'none'
								});
							}
						} else {
							this.form.model = '';
							this.form.color = '';
							this.form.createTime = '';
							uni.showToast({
								icon: 'none',
								title: '查询失败'
							});
						}
						
						// 请求 是否保修接口
						// let new=this.form.insurance
						// console.log(111)
						// console.log(this.form)
						// this.getOrderReport(e).then(res=>{
						// 	console.log(res)
						// 	if(res.result==false){
								
						// 	}
						// })
						this.updateScanDeviceInfo(this.form);
						this.$emit('value-change', { isValid });
					})
					.catch(err => {
						console.log(err);
						uni.hideLoading();
					});
			} else {
				this.form.model = '';
				this.form.color = '';
				this.form.createTime = '';
				this.form.code = '';
				this.updateScanDeviceInfo(this.form);
				this.$emit('value-change', { isValid: false });
			}
		},
		
		
		getProductFromServer(code) {
			this.getDeviceByCustomer({
				code
			})
				.then(res => {
					console.log(res);
					if (res && res.code == 0) {
						console.log(567);
						console.log(res.result);
						this.form.insurance=res.result;
				        if(res.result==true){
							this.form.insurance='是';
							}else {
								this.form.insurance='否';
							}
						console.log(this.form.insurance)
						if (res.data && res.data.saleTime) {
							this.form.saleTime = res.data.saleTime;
							
						} else {
							this.form.saleTime = '';
						}
						if (res.data && res.data.customer) {
							this.getAgentByTeamNo({
								teamNo: res.data.customer,
							})
								.then(res => {
									console.log(res);
									if (res && res.code == 0 && res.data && res.data[0]) {
										this.form.saleDealer = res.data[0].Name;
									} else {
										this.form.saleDealer = '';
									}
								})
								.catch(err => {
									console.log(err);
									this.form.saleDealer = '';
								});
						}
					}
				})
				.catch(err => {
					console.log(err);
					this.form.saleTime = '';
					this.form.saleDealer = '';
				});
		}
	}
};
</script>

<style></style>
